package com.sunny.blog.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.extra.mail.MailUtil;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.response.OapiRobotSendResponse;
import com.sunny.blog.common.entity.SysConstant;
import com.sunny.blog.service.ICacheService;
import com.sunny.blog.service.SmsSendService;
import com.taobao.api.ApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName: SmsSendServiceImpl
 * @Description:
 * @Author: sunt
 * @Date: 2020/4/28 17:14
 * @Version 1.0
 **/
@Service
public class SmsSendServiceImpl implements SmsSendService {

    @Value("${dingtalk.robot_token}")
    private String dingRobottalk;
    @Autowired
    private ICacheService cacheService;

    @Override
    public OapiRobotSendResponse sendDingTextMsg(String content, List<String> atMobiles, boolean isAtAll) throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient(dingRobottalk);
        OapiRobotSendRequest request = new OapiRobotSendRequest();
        request.setMsgtype("text");//消息类型
        OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
        text.setContent("【系统通知】:" + content);
        request.setText(text);
        OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
        if(CollectionUtil.isNotEmpty(atMobiles)) {
            at.setAtMobiles(atMobiles);
        }
        at.setIsAtAll(String.valueOf(isAtAll));
        request.setAt(at);
        return client.execute(request);
    }

    @Override
    public void sendMailCode(String to) {
        String subject = "[SunnyBlog博客]邮件验证码";
        String mailCode = RandomUtil.randomNumbers(6);
        cacheService.cacheStringData(SysConstant.MAIL_CODE_CACHE + to + mailCode,mailCode,10);
        String content = "<head>\n" +
                "    <base target=\"_blank\" />\n" +
                "    <style type=\"text/css\">::-webkit-scrollbar{ display: none; }</style>\n" +
                "    <style id=\"cloudAttachStyle\" type=\"text/css\">#divNeteaseBigAttach, #divNeteaseBigAttach_bak{display:none;}</style>\n" +
                "    <style id=\"blockquoteStyle\" type=\"text/css\">blockquote{display:none;}</style>\n" +
                "    <style type=\"text/css\">\n" +
                "        body{font-size:14px;font-family:arial,verdana,sans-serif;line-height:1.666;padding:0;margin:0;overflow:auto;white-space:normal;word-wrap:break-word;min-height:100px}\n" +
                "        td, input, button, select, body{font-family:Helvetica, 'Microsoft Yahei', verdana}\n" +
                "        pre {white-space:pre-wrap;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;word-wrap:break-word;width:95%}\n" +
                "        th,td{font-family:arial,verdana,sans-serif;line-height:1.666}\n" +
                "        img{ border:0}\n" +
                "        header,footer,section,aside,article,nav,hgroup,figure,figcaption{display:block}\n" +
                "        blockquote{margin-right:0px}\n" +
                "    </style>\n" +
                "</head>\n" +
                "<body tabindex=\"0\" role=\"listitem\">\n" +
                "<table width=\"700\" border=\"0\" align=\"center\" cellspacing=\"0\" style=\"width:700px;\">\n" +
                "    <tbody>\n" +
                "    <tr>\n" +
                "        <td>\n" +
                "            <div style=\"width:700px;margin:0 auto;border-bottom:1px solid #ccc;margin-bottom:30px;\">\n" +
                "                <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"700\" height=\"39\" style=\"font:12px Tahoma, Arial, 宋体;\">\n" +
                "                    <tbody><tr><td width=\"210\"></td></tr></tbody>\n" +
                "                </table>\n" +
                "            </div>\n" +
                "            <div style=\"width:680px;padding:0 10px;margin:0 auto;\">\n" +
                "                <div style=\"line-height:1.5;font-size:14px;margin-bottom:25px;color:#4d4d4d;\">\n" +
                "                    <strong style=\"display:block;margin-bottom:15px;\">尊敬的用户：<span style=\"color:#f60;font-size: 16px;\"></span>您好！</strong>\n" +
                "                    <strong style=\"display:block;margin-bottom:15px;\">\n" +
                "                        您正在进行<span style=\"color: red\">邮箱验证</span>操作，本次请求的验证码为：<span style=\"color:#f60;font-size: 24px\">"+mailCode+"</span>\n" +
                "                    </strong>\n" +
                "                </div>\n" +
                "                <div style=\"margin-bottom:30px;\">\n" +
                "                    <small style=\"display:block;margin-bottom:20px;font-size:12px;\">\n" +
                "                        <p style=\"color:#747474;\">\n" +
                "                            注意：为了保障您帐号的安全性，请在10分钟内完成验证\n" +
                "                            <br>（系统管理员不会向你索取此验证码，请勿泄漏！)\n" +
                "                        </p>\n" +
                "                    </small>\n" +
                "                </div>\n" +
                "            </div>\n" +
                "            <div style=\"width:700px;margin:0 auto;\">\n" +
                "                <div style=\"padding:10px 10px 0;border-top:1px solid #ccc;color:#747474;margin-bottom:20px;line-height:1.3em;font-size:12px;\">\n" +
                "                    <p>此为系统邮件，请勿回复<br>\n" +
                "                        请保管好您的邮箱，避免账号被他人盗用\n" +
                "                    </p>\n" +
                "                    <p>SunnyBlog博客系统</p>\n" +
                "                </div>\n" +
                "            </div>\n" +
                "        </td>\n" +
                "    </tr>\n" +
                "    </tbody>\n" +
                "</table>\n" +
                "</body>\n";
        MailUtil.send(to,subject,content,true);
    }

    @Override
    public void sendMailNotice(String to, String content) {
        String context = " String content = \"<head>\\n\" +\n" +
                "                \"    <base target=\\\"_blank\\\" />\\n\" +\n" +
                "                \"    <style type=\\\"text/css\\\">::-webkit-scrollbar{ display: none; }</style>\\n\" +\n" +
                "                \"    <style id=\\\"cloudAttachStyle\\\" type=\\\"text/css\\\">#divNeteaseBigAttach, #divNeteaseBigAttach_bak{display:none;}</style>\\n\" +\n" +
                "                \"    <style id=\\\"blockquoteStyle\\\" type=\\\"text/css\\\">blockquote{display:none;}</style>\\n\" +\n" +
                "                \"    <style type=\\\"text/css\\\">\\n\" +\n" +
                "                \"        body{font-size:14px;font-family:arial,verdana,sans-serif;line-height:1.666;padding:0;margin:0;overflow:auto;white-space:normal;word-wrap:break-word;min-height:100px}\\n\" +\n" +
                "                \"        td, input, button, select, body{font-family:Helvetica, 'Microsoft Yahei', verdana}\\n\" +\n" +
                "                \"        pre {white-space:pre-wrap;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;word-wrap:break-word;width:95%}\\n\" +\n" +
                "                \"        th,td{font-family:arial,verdana,sans-serif;line-height:1.666}\\n\" +\n" +
                "                \"        img{ border:0}\\n\" +\n" +
                "                \"        header,footer,section,aside,article,nav,hgroup,figure,figcaption{display:block}\\n\" +\n" +
                "                \"        blockquote{margin-right:0px}\\n\" +\n" +
                "                \"    </style>\\n\" +\n" +
                "                \"</head>\\n\" +\n" +
                "                \"<body tabindex=\\\"0\\\" role=\\\"listitem\\\">\\n\" +\n" +
                "                \"<table width=\\\"700\\\" border=\\\"0\\\" align=\\\"center\\\" cellspacing=\\\"0\\\" style=\\\"width:700px;\\\">\\n\" +\n" +
                "                \"    <tbody>\\n\" +\n" +
                "                \"    <tr>\\n\" +\n" +
                "                \"        <td>\\n\" +\n" +
                "                \"            <div style=\\\"width:700px;margin:0 auto;border-bottom:1px solid #ccc;margin-bottom:30px;\\\">\\n\" +\n" +
                "                \"                <table border=\\\"0\\\" cellpadding=\\\"0\\\" cellspacing=\\\"0\\\" width=\\\"700\\\" height=\\\"39\\\" style=\\\"font:12px Tahoma, Arial, 宋体;\\\">\\n\" +\n" +
                "                \"                    <tbody><tr><td width=\\\"210\\\"></td></tr></tbody>\\n\" +\n" +
                "                \"                </table>\\n\" +\n" +
                "                \"            </div>\\n\" +\n" +
                "                \"            <div style=\\\"width:680px;padding:0 10px;margin:0 auto;\\\">\\n\" +\n" +
                "                \"                <div style=\\\"line-height:1.5;font-size:14px;margin-bottom:25px;color:#4d4d4d;\\\">\\n\" +\n" +
                "                \"                    <strong style=\\\"display:block;margin-bottom:15px;\\\">尊敬的用户：<span style=\\\"color:#f60;font-size: 16px;\\\"></span>您好！</strong>\\n\" +\n" +
                "                \"                    <strong style=\\\"display:block;margin-bottom:15px;\\\">\\n\" +\n" +
                "                \"                        "+content+" +\n" +
                "                \"                    </strong>\\n\" +\n" +
                "                \"                </div>\\n\" +\n" +
                "                \"                <div style=\\\"margin-bottom:30px;\\\">\\n\" +\n" +
                "                \"                    <small style=\\\"display:block;margin-bottom:20px;font-size:12px;\\\">\\n\" +\n" +
                "                \"                        <p style=\\\"color:#747474;\\\">\\n\" +\n" +
                "                \"                            注意：为了保障您帐号的安全性，请在10分钟内完成验证\\n\" +\n" +
                "                \"                            <br>（系统管理员不会向你索取此验证码，请勿泄漏！)\\n\" +\n" +
                "                \"                        </p>\\n\" +\n" +
                "                \"                    </small>\\n\" +\n" +
                "                \"                </div>\\n\" +\n" +
                "                \"            </div>\\n\" +\n" +
                "                \"            <div style=\\\"width:700px;margin:0 auto;\\\">\\n\" +\n" +
                "                \"                <div style=\\\"padding:10px 10px 0;border-top:1px solid #ccc;color:#747474;margin-bottom:20px;line-height:1.3em;font-size:12px;\\\">\\n\" +\n" +
                "                \"                    <p>此为系统邮件，请勿回复<br>\\n\" +\n" +
                "                \"                        请保管好您的邮箱，避免账号被他人盗用\\n\" +\n" +
                "                \"                    </p>\\n\" +\n" +
                "                \"                    <p>SunnyBlog博客系统</p>\\n\" +\n" +
                "                \"                </div>\\n\" +\n" +
                "                \"            </div>\\n\" +\n" +
                "                \"        </td>\\n\" +\n" +
                "                \"    </tr>\\n\" +\n" +
                "                \"    </tbody>\\n\" +\n" +
                "                \"</table>\\n\" +\n" +
                "                \"</body>\\n\";";

        MailUtil.send(to,"SunnyBlog博客系统通知",content,true);
    }
}
